package com.xjscrm.console.chat.queue;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author mingyang.chen create in 2024/3/25 19:02
 */
@Data
public class ChatDelayTask implements Delayed {

    private final Long merId;
    private final Long staffId;
    private final Long talkSessionId;
    private final Long delay;
    //
    private final Runnable runnable;

    public ChatDelayTask(Long merId, Long staffId, Long talkSessionId, Long delay, Runnable runnable) {
        this.merId = merId;
        this.staffId = staffId;
        this.talkSessionId = talkSessionId;
        this.delay = delay * 1000L + System.currentTimeMillis();
        this.runnable = runnable;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delay - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ChatDelayTask that = (ChatDelayTask) o;

        if (!merId.equals(that.merId)) return false;
        if (!staffId.equals(that.staffId)) return false;
        return talkSessionId.equals(that.talkSessionId);
    }

    @Override
    public int hashCode() {
        int result = merId.hashCode();
        result = 31 * result + staffId.hashCode();
        result = 31 * result + talkSessionId.hashCode();
        return result;
    }
}
